package org.tedu.cloudnote.serviceImpl;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.tedu.cloudnote.dao.UserDao;
import org.tedu.cloudnote.entity.User;
import org.tedu.cloudnote.service.UserService;
import org.tedu.cloudnote.util.NoteResult;
import org.tedu.cloudnote.util.NoteUtil;

@Service("userService")
public class UserServiceImpl implements UserService{
	@Resource
	private UserDao dao;
	public NoteResult checkLogin(String username, String password) {
		NoteResult result=new NoteResult();
		User user=dao.findByUserName(username);
		if(user==null){
			result.setStatus(1);
			result.setMsg("用户名错误！");
		}else if(NoteUtil.Md5(password).equals(user.getCn_user_password())){
			result.setStatus(0);
			result.setMsg("登入成功");
			result.setData(user.getCn_user_id());
		}else {
			result.setStatus(2);
			result.setMsg("密码错误");
		}
		return result;
	}
	public NoteResult saveRegist(String username, String password, String nick) {
		NoteResult result=new NoteResult();
		User has_user=dao.findByUserName(username);
		if(has_user!=null){
			result.setStatus(1);
			result.setMsg("用户名已存在");
		return result;
		}
		User user=new User();
		user.setCn_user_name(username);
		user.setCn_user_nick(nick);
		user.setCn_user_password(NoteUtil.Md5(password));
		user.setCn_user_token(null);
		user.setCn_user_id(NoteUtil.createId());
		dao.save(user);
		result.setStatus(0);
		result.setMsg("注册成功");
		result.setData(user);
		return result;
	}
	
	

}
